package com.yanqu.road.server.cmd.activity.yearMaster;

import com.yanqu.road.entity.activity.yearMaster.UserYearMasterDataResult;
import com.yanqu.road.logic.pb.YearMasterPb;
import com.yanqu.road.pb.yearMaster.YearMasterProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.yearMaster.YearMasterModule;
import com.yanqu.road.server.manger.activity.yearMaster.YearMasterMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;


@Cmd(code = GameProtocol.S_YEAR_MASTER_ATTACK_FROM_CROSS, desc = "瑞兽降福攻击")
public class YearAttackBossFromCross extends AbstractCommand {

    private int activityId;
    private int addScore;
    private int resultRet;
    private int gridType;
    private int gridNum;
    private String removeGrid;
    private int myRank;
    private int mode;
    private UserYearMasterDataResult userYearMasterDataResult;
    private YearMasterProto.YearMasterBossInfo bossInfo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        YearMasterProto.AttackBossFromCross msg = YearMasterProto.AttackBossFromCross.parseFrom(packet.getBytes());

        activityId = msg.getActivityId();
        addScore = msg.getAddScore();
        resultRet = msg.getRet();
        bossInfo = msg.getBossInfo();
        gridType = msg.getGridType();
        gridNum = msg.getGridNum();
        mode = msg.getGameMode();
        removeGrid = msg.getRemoveGrid();
        myRank = msg.getMyRank();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        YearMasterProto.YearMasterChooseGridRspMsg.Builder response = YearMasterProto.YearMasterChooseGridRspMsg.newBuilder();
        if (resultRet != 0) {
            response.setRet(resultRet);
            player.sendPacket(ClientProtocol.U_YEAR_MASTER_GRID_CHOOSE, response);
        }
        userYearMasterDataResult = player.getModule(YearMasterModule.class).playGameCrossHandle(activityId, addScore, bossInfo,gridType,gridNum,mode);
    }

    @Override
    public void sendData(GamePlayer player) {
        YearMasterProto.YearMasterChooseGridRspMsg.Builder resp = YearMasterProto.YearMasterChooseGridRspMsg.newBuilder();

        if (userYearMasterDataResult != null) {
            resp.setRet(userYearMasterDataResult.getRet());
            resp.setRewards(userYearMasterDataResult.getRewords());
            resp.setScore(userYearMasterDataResult.getUserYearMasterData().getScore());
            resp.setBaseInfo(YearMasterPb.parseYearMasterBaseRespMsg(userYearMasterDataResult.getUserYearMasterData(), bossInfo,YearMasterMgr.getYearMasterActivityConfig().getHighOpenScore()));
            resp.setUserRank(YearMasterMgr.getYearMasterUserRank(player.getUserId(),myRank));
            resp.setRemoveGrid(removeGrid);
        }
        player.sendPacket(ClientProtocol.U_YEAR_MASTER_GRID_CHOOSE, resp);

    }
}
